package com.example.month11.fragment1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;

import com.example.month11.fragment1.fragment.GiftFragment;
import com.example.month11.fragment1.fragment.MyDialogFragment;
import com.example.month11.fragment1.fragment.OrderFragment;
import com.example.month11.fragment1.fragment.ShareFragment;
import com.example.month11.fragment1.fragment.ShopRankFragment;
import com.example.recyclerview01.R;

public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentManager manager;
    private FragmentTransaction transaction;
    private RadioButton rb_home, rb_share, rb_gift, rb_order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题栏
        setContentView(R.layout.activity_fragment);
        initViews();

        // 实现仿微信效果。点击切换不同的fragment
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.content_layout, new ShopRankFragment());
        transaction.commit();
    }

    private void initViews() {
        rb_home = findViewById(R.id.rb_home);
        rb_share = findViewById(R.id.rb_share);
        rb_gift = findViewById(R.id.rb_gift);
        rb_order = findViewById(R.id.rb_order);

        rb_home.setOnClickListener(this);
        rb_share.setOnClickListener(this);
        rb_gift.setOnClickListener(this);
        rb_order.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        transaction = manager.beginTransaction();
        switch (v.getId()) {
            case R.id.rb_home:
                showDialog();
                transaction.replace(R.id.content_layout, new ShopRankFragment());
                break;
            case R.id.rb_share:
                showOldDialog();
                transaction.replace(R.id.content_layout, new ShareFragment());
                break;
            case R.id.rb_gift:
                transaction.replace(R.id.content_layout, new GiftFragment());
                break;
            case R.id.rb_order:
                transaction.replace(R.id.content_layout, new OrderFragment());
                break;
        }
        transaction.commit();
    }

    private void showOldDialog() {
        // 旋转屏幕后，由于Activity会销毁重新创建，导致dialog不可见了。
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("title").setMessage("this is message");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("取消", null);
        builder.create().show();
    }

    // 弹出对话框。
    private void showDialog() {
        MyDialogFragment dialogFragment = new MyDialogFragment();
        dialogFragment.show(getSupportFragmentManager(), "dialog");
    }

}
